import type { NextApiRequest } from 'next'

import type { Response as Res } from '@/types/type'

export class Response {
  success<T>({ data, code = 200, message = 'ok' }: Res<T>) {
    return {
      code,
      time: Date.now() + (1000 * 60 * 60 * 8).toString(),
      message,
      success: true,
      data
    }
  }

  fail({ code = 400, message = 'error' }: Res<unknown>, req: NextApiRequest) {
    return {
      code,
      message,
      success: false,
      time: Date.now() + (1000 * 60 * 60 * 8).toString(),
      data: {
        url: req.url,
        method: req.method,
        [Object.keys(req.query).length ? 'query' : Symbol.for('query')]:
          req.query,
        [Object.keys(req.body).length ? 'body' : Symbol.for('body')]: req.body
      }
    }
  }
}

export const response = new Response()
